

package Statistics;


// Referenced classes of package Statistics:
//            Descriptives, Matrix

public class Multivariate
{

    public Multivariate(Matrix A)
    {
        this.A = A;
    }

    public Matrix CovarianceMatrix()
    {
        Matrix temp = new Matrix(A.myData);
        Matrix ret = new Matrix(A.m, A.m);
        Descriptives d = new Descriptives(temp);
        Matrix meanVector = d.getMean();
        for(int i = 0; i < A.n; i++)
        {
            for(int j = 0; j < A.m; j++)
                temp.myData[i][j] = temp.myData[i][j] - meanVector.myData[0][j];

        }

        return temp.Transpose().Mult(temp).scalerMult(1.0D / ((double)A.n - 1.0D));
    }

    public Matrix Mahalanobis()
    {
        Matrix temp = new Matrix(A.myData);
        Matrix cov = CovarianceMatrix();
        Matrix inv = cov.Inverse();
        Matrix ret = new Matrix(1, A.n);
        Descriptives d = new Descriptives(temp);
        Matrix meanVector = d.getMean();
        Matrix row = new Matrix(1, A.m);
        for(int i = 0; i < A.n; i++)
        {
            row = A.getRow(i);
            ret.myData[0][i] = row.Diff(meanVector).Mult(inv).Mult(row.Diff(meanVector).Transpose()).myData[0][0];
        }

        return ret;
    }

    public Matrix A;
}